package net.esj.basic.utils.web;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Pattern;

import net.esj.basic.annotation.check.MaxLength;
import net.esj.basic.annotation.check.Regular;
import net.esj.basic.annotation.check.Required;
import net.esj.basic.annotation.check.Verify;
import net.esj.basic.core.engine.support.validator.exception.MelonValidateException;
import net.esj.basic.pojo.regular.RegularEnum;
import net.esj.basic.utils.Language;
import net.esj.basic.utils.Validators;

import org.apache.commons.beanutils.BeanUtilsBean;

@Deprecated
public class PojoFieldChecker {

	public static void Check(Object o) throws MelonValidateException{
		String error = "";
		PropertyDescriptor[] origDescriptors = BeanUtilsBean.getInstance()
			.getPropertyUtils().getPropertyDescriptors(o);
		
			for (int i = 0; i < origDescriptors.length; i++) {
				String name = origDescriptors[i].getName();
				if ("class".equals(name)) {
					continue;
				}
				Field field;
				try {
					Object value = 
						BeanUtilsBean.getInstance().getPropertyUtils().getSimpleProperty(o, name);
					field = o.getClass().getDeclaredField(name);
					Verify verify = field.getAnnotation(Verify.class);
					if(verify!=null){
						error +=checkRequired(verify, value,field);
						error +=checkMaxLength(verify, value, field);
						error +=checkRegular(verify, value, field);
					}
				} catch (NoSuchFieldException e) {
				} catch (IllegalAccessException e) {
				} catch (InvocationTargetException e) {
				} catch (NoSuchMethodException e) {
				}
			}
		if(!Validators.isEmpty(error)){
			throw new MelonValidateException(error,null);
		}
	}
	
	private static String checkRequired(Verify verify,Object value,Field field){
		Required required = verify.required();
		if(required==null){
			return "";
		}
		if(required.value() && Validators.isEmpty(value)){
			return generateError(field, "required", required.show());
		}
		return "";
	}
	
	private static String checkMaxLength(Verify verify,Object value,Field field){
		if(Validators.isEmpty(value)){
			return "";
		}
		MaxLength maxLength = verify.maxLength();
		if(maxLength!=null && value.toString().length()>maxLength.value()){
			return generateError(field, "maxLength", maxLength.show());
		}
		return "";
	}
	
	private static String checkRegular(Verify verify,Object value,Field field){
		if(Validators.isEmpty(value)){
			return "";
		}
		Regular regular  = verify.regular();
		if(regular==null){
			return "";
		}
		RegularEnum[] enums = regular.regulars();
		if(enums!=null){
			for(RegularEnum e : enums){
				if(!Pattern.matches(e.getExpression(), value.toString())){
					return generateError( field, "regular", regular.show());
				}
			}
		}
		
		String[] expressions = regular.expression();
		for(String str : expressions){
			if(!Pattern.matches(str, value.toString())){
				return  generateError( field, "regular", regular.show());
			}
		}
		return "";
	}
	
	private static String generateError(Field field,String valid,String show){
		String err ="";
		if("default_key".equals(show)){
			String key =field.getDeclaringClass().getName()+"."+ field.getName()+"."+valid+".show";
			String s = Language.getString(key);
			if(!Validators.isEmpty(s)){
				err =  s+"\r\n";
			}
		}else{
			err = show+"\r\n";
		}
		return err;
	}
}
